use crate::window::keyboard::{unsupported_key, Token};
use skulpin::sdl2::keyboard::Keycode::{self, *};

/// Maps winit keyboard events to Vim tokens
pub fn handle_qwerty_layout(keycode: Keycode, shift: bool) -> Option<Token<'static>> {
    let special = |text| Some(Token::new(text, true, true));
    let normal = |text| Some(Token::new(text, false, true));
    let partial = |text| Some(Token::new(text, false, false));
    match (keycode, shift) {
        (Backspace, _) => special("BS"),
        (Tab, _) => special("Tab"),
        (Return, _) => special("Enter"),
        (Escape, _) => special("Esc"),
        (Space, _) => normal(" "),
        (Exclaim, _) => normal("!"),
        (Quotedbl, _) => normal("\""),
        (Hash, _) => normal("#"),
        (Dollar, _) => normal("$"),
        (Percent, _) => normal("%"),
        (Ampersand, _) => normal("&"),
        (Quote, false) => normal("'"),
        (Quote, true) => normal("\""),
        (LeftParen, _) => normal("("),
        (RightParen, _) => normal(")"),
        (Asterisk, _) => normal("*"),
        (Plus, _) => normal("+"),
        (Comma, false) => normal(","),
        (Comma, true) => special("lt"),
        (Minus, false) => partial("-"),
        (Minus, true) => partial("_"),
        (Period, false) => partial("."),
        (Period, true) => partial(">"),
        (Slash, false) => partial("/"),
        (Slash, true) => partial("?"),
        (Num0, false) => partial("0"),
        (Num0, true) => special(")"),
        (Num1, false) => partial("1"),
        (Num1, true) => special("!"),
        (Num2, false) => partial("2"),
        (Num2, true) => partial("@"),
        (Num3, false) => partial("3"),
        (Num3, true) => partial("#"),
        (Num4, false) => partial("4"),
        (Num4, true) => partial("$"),
        (Num5, false) => partial("5"),
        (Num5, true) => partial("%"),
        (Num6, false) => partial("6"),
        (Num6, true) => partial("^"),
        (Num7, false) => partial("7"),
        (Num7, true) => partial("&"),
        (Num8, false) => partial("8"),
        (Num8, true) => partial("*"),
        (Num9, false) => partial("9"),
        (Num9, true) => partial("("),
        (Colon, _) => normal(":"),
        (Semicolon, false) => partial(";"),
        (Semicolon, true) => partial(":"),
        (Less, _) => special("lt"),
        (Equals, false) => partial("="),
        (Equals, true) => partial("+"),
        (Greater, _) => normal("gt"),
        (Question, _) => normal("?"),
        (At, _) => normal("@"),
        (LeftBracket, false) => partial("["),
        (LeftBracket, true) => partial("{"),
        (Backslash, false) => partial("\\"),
        (Backslash, true) => partial("|"),
        (RightBracket, false) => partial("]"),
        (RightBracket, true) => partial("}"),
        (Caret, _) => normal("^"),
        (Underscore, _) => normal("_"),
        (Backquote, false) => partial("`"),
        (Backquote, true) => partial("~"),
        (A, _) => normal("a"),
        (B, _) => normal("b"),
        (C, _) => normal("c"),
        (D, _) => normal("d"),
        (E, _) => normal("e"),
        (F, _) => normal("f"),
        (G, _) => normal("g"),
        (H, _) => normal("h"),
        (I, _) => normal("i"),
        (J, _) => normal("j"),
        (K, _) => normal("k"),
        (L, _) => normal("l"),
        (M, _) => normal("m"),
        (N, _) => normal("n"),
        (O, _) => normal("o"),
        (P, _) => normal("p"),
        (Q, _) => normal("q"),
        (R, _) => normal("r"),
        (S, _) => normal("s"),
        (T, _) => normal("t"),
        (U, _) => normal("u"),
        (V, _) => normal("v"),
        (W, _) => normal("w"),
        (X, _) => normal("x"),
        (Y, _) => normal("y"),
        (Z, _) => normal("z"),
        (Delete, _) => special("Delete"),
        (F1, _) => special("F1"),
        (F2, _) => special("F2"),
        (F3, _) => special("F3"),
        (F4, _) => special("F4"),
        (F5, _) => special("F5"),
        (F6, _) => special("F6"),
        (F7, _) => special("F7"),
        (F8, _) => special("F8"),
        (F9, _) => special("F9"),
        (F10, _) => special("F10"),
        (F11, _) => special("F11"),
        (F12, _) => special("F12"),
        (Insert, _) => special("Insert"),
        (Home, _) => special("Home"),
        (PageUp, _) => special("PageUp"),
        (End, _) => special("End"),
        (PageDown, _) => special("PageDown"),
        (Right, _) => special("Right"),
        (Left, _) => special("Left"),
        (Down, _) => special("Down"),
        (Up, _) => special("Up"),
        (KpDivide, _) => special("/"),
        (KpMultiply, _) => special("*"),
        (KpMinus, _) => special("-"),
        (KpPlus, _) => special("+"),
        (KpEnter, _) => special("Enter"),
        (Kp0, _) => normal("0"),
        (Kp1, _) => normal("1"),
        (Kp2, _) => normal("2"),
        (Kp3, _) => normal("3"),
        (Kp4, _) => normal("4"),
        (Kp5, _) => normal("5"),
        (Kp6, _) => normal("6"),
        (Kp7, _) => normal("7"),
        (Kp8, _) => normal("8"),
        (Kp9, _) => normal("9"),
        (KpPeriod, _) => normal("."),
        (KpEquals, _) => normal("="),
        (F13, _) => special("F13"),
        (F14, _) => special("F14"),
        (F15, _) => special("F15"),
        (F16, _) => special("F16"),
        (F17, _) => special("F17"),
        (F18, _) => special("F18"),
        (F19, _) => special("F19"),
        (F20, _) => special("F20"),
        (F21, _) => special("F21"),
        (F22, _) => special("F22"),
        (F23, _) => special("F23"),
        (F24, _) => special("F24"),
        (KpLeftParen, _) => normal("("),
        (KpRightParen, _) => normal("("),
        (KpLeftBrace, _) => normal("["),
        (KpRightBrace, _) => normal("]"),
        (KpTab, _) => special("TAB"),
        (KpBackspace, _) => special("BS"),
        (KpA, _) => normal("A"),
        (KpB, _) => normal("B"),
        (KpC, _) => normal("C"),
        (KpD, _) => normal("D"),
        (KpE, _) => normal("E"),
        (KpF, _) => normal("F"),
        (KpPower, _) => normal("^"),
        (KpPercent, _) => normal("%"),
        (KpLess, _) => special("lt"),
        (KpGreater, _) => special("gt"),
        (KpAmpersand, _) => normal("&"),
        (KpVerticalBar, _) => normal("|"),
        (KpColon, _) => normal(":"),
        (KpHash, _) => normal("#"),
        (KpSpace, _) => normal(" "),
        (KpAt, _) => normal("@"),
        (KpExclam, _) => normal("!"),
        (LCtrl, _) => None,
        (LShift, _) => None,
        (LAlt, _) => None,
        (LGui, _) => None,
        (RCtrl, _) => None,
        (RShift, _) => None,
        (RAlt, _) => None,
        (RGui, _) => None,
        (keycode, _) => unsupported_key(keycode),
    }
}
